(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.bu.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'buService' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, buService) {
		var ctrl = this, buInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			var params = {
					buCode : buInfo.buCode
			};
			ctrl.childInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.child.tableConfigs.table);
			ctrl.childInnerGridOptions.data = 'vm.bu.buChilds';
			//
			ctrl.ctrlInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.ctrl.tableConfigs.table);
			ctrl.ctrlInnerGridOptions.data = 'vm.bu.buCtrls';
			//
			buService.initOtherData($scope, ctrl);
			authService.view(params).then(function(response) {
				ctrl.bu = response.data || buInfo;
				//
				buService.getBuTypeOptions().then(function(response) {
					ctrl.buTypeOptions = response;
					// need init currBuCtrlCat/currAllowChild/currBuCtrlProdFlag
					if (ctrl.bu.buType) {
						for (var i = 0; i < ctrl.buTypeOptions.length; i++) {
							var _buType = ctrl.buTypeOptions[i];
							if (_buType.value == ctrl.bu.buType) {
								ctrl.bu.currBuCtrlCat = _buType.extProperty.buCtrlCat;
								ctrl.bu.currAllowChild = _buType.extProperty.allowChild;
								ctrl.bu.currBuCtrlProdFlag = _buType.extProperty.buCtrlProdFlag;
								buService.changeBuCtrlTabTitle($scope, ctrl, true);
							}
						}
					}
				}, angular.noop);
			});
		};
		

		
	}
})(angular);